package top.lixunda.ecommerce.server.user.api.entity.dto;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.ImmutableMap;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.ecommerce.server.business.api.condition.ILambdaQueryWrapperDTO;
import top.lixunda.ecommerce.server.business.api.entity.dto.OrderByDTO;
import top.lixunda.ecommerce.server.user.api.entity.db.PermissionDO;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 权限查询DTO
 *
 * @author lixunda
 * @version time:2020/4/24 15:04
 */
@ApiModel("权限查询DTO")
@Data
@EqualsAndHashCode(callSuper = true)
public class PermissionQueryDTO extends OrderByDTO
        implements ILambdaQueryWrapperDTO<PermissionDO> {

    /**
     * 搜索字段
     */
    @ApiModelProperty(value = "搜索字段")
    private List<String> search;

    /**
     * 分配编号，唯一主键
     */
    @ApiModelProperty(value = "分配编号，唯一主键")
    private List<Integer> permissionId;

    /**
     * 权限编号，真正的权限判定标志，不能重复
     */
    @ApiModelProperty(value = "权限编号，真正的权限判定标志，不能重复")
    private List<String> permissionCode;

    /**
     * 权限名称，只是用来查看
     */
    @ApiModelProperty(value = "权限名称，只是用来查看")
    private List<String> permissionName;

    /**
     * 创建模式，自动创建和手动创建
     */
    @ApiModelProperty(value = "创建模式，自动创建和手动创建")
    private List<String> createMode;

    /**
     * 创建时间
     */
    @ApiModelProperty(value = "创建时间")
    private LocalDateTime createTimeBegin;

    /**
     * 创建时间
     */
    @ApiModelProperty(value = "创建时间")
    private LocalDateTime createTimeEnd;

    /**
     * 创建人员编号
     */
    @ApiModelProperty(value = "创建人员编号")
    private List<Integer> createUserId;

    /**
     * 创建人员类型
     */
    @ApiModelProperty(value = "创建人员类型")
    private List<String> createUserType;

    /**
     * 排序字段映射
     */
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private static final Map<String, SFunction<PermissionDO, ?>> COLUMNS =
            new ImmutableMap.Builder<String, SFunction<PermissionDO, ?>>()
                    .put("permissionId", PermissionDO::getPermissionId)
                    .put("permissionCode", PermissionDO::getPermissionCode)
                    .put("permissionName", PermissionDO::getPermissionName)
                    .put("permissionDesc", PermissionDO::getPermissionDesc)
                    .put("createMode", PermissionDO::getCreateMode)
                    .put("createTime", PermissionDO::getCreateTime)
                    .put("createUserId", PermissionDO::getCreateUserId)
                    .put("createUserName", PermissionDO::getCreateUserName)
                    .put("createUserType", PermissionDO::getCreateUserType)
                    .build();

    /**
     * 将当前用于查询的DTO中的查询数据转换为 LambdaQuerySqlWrapper
     *
     * @return 条件构造器 LambdaQuerySqlWrapper
     */
    @Override
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    public LambdaQueryWrapper<PermissionDO> toWrapper() {
        LambdaQueryWrapper<PermissionDO> wrapper = new LambdaQueryWrapper<>();
        if (ObjectValidators.isNotEmpty(search)) {
            wrapper.and(innerWrapper -> search.forEach(item ->
                    innerWrapper.or().like(PermissionDO::getPermissionCode, item)
                            .or().like(PermissionDO::getPermissionName, item)
                            .or().like(PermissionDO::getPermissionDesc, item)));
        }
        if (ObjectValidators.isNotEmpty(permissionId)) {
            wrapper.and(innerWrapper -> permissionId.forEach(item ->
                    innerWrapper.or().eq(PermissionDO::getPermissionId, item)));
        }
        if (ObjectValidators.isNotEmpty(permissionCode)) {
            wrapper.and(innerWrapper -> permissionCode.forEach(item ->
                    innerWrapper.or().eq(PermissionDO::getPermissionCode, item)));
        }
        if (ObjectValidators.isNotEmpty(permissionName)) {
            wrapper.and(innerWrapper -> permissionName.forEach(item ->
                    innerWrapper.or().eq(PermissionDO::getPermissionName, item)));
        }
        if (ObjectValidators.isNotEmpty(createMode)) {
            wrapper.and(innerWrapper -> createMode.forEach(item ->
                    innerWrapper.or().eq(PermissionDO::getCreateMode, item)));
        }
        if (ObjectValidators.isNotEmpty(createTimeBegin)) {
            wrapper.ge(PermissionDO::getCreateTime, createTimeBegin);
        }
        if (ObjectValidators.isNotEmpty(createTimeEnd)) {
            wrapper.le(PermissionDO::getCreateTime, createTimeEnd);
        }
        if (ObjectValidators.isNotEmpty(createUserId)) {
            wrapper.and(innerWrapper -> createUserId.forEach(item ->
                    innerWrapper.or().eq(PermissionDO::getCreateUserId, item)));
        }
        if (ObjectValidators.isNotEmpty(createUserType)) {
            wrapper.and(innerWrapper -> createUserType.forEach(item ->
                    innerWrapper.or().eq(PermissionDO::getCreateUserType, item)));
        }
        dealWithOrderBy(wrapper, COLUMNS);
        return wrapper;
    }
}
